<?php

namespace RSP\ReseauSocialBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use \RSP\ReseauSocialBundle\Entity\Message;
use \RSP\ReseauSocialBundle\Form\MessageType;

class MessageController extends Controller {
    
    public function listeAction() {
        $em = $this->getDoctrine()->getEntityManager();
        $repository = $em->getRepository('RSPReseauSocialBundle:message');
        $messages=$repository->findAll();
        return $this->render('RSPReseauSocialBundle:Message:liste.html.twig',  array('Messages' => $messages));
    }
    public function newAction() {
        $message = new Message();
        $form = $this->createForm(new MessageType(), $message);
        $request = $this->get('request');
        if ($request->isMethod('Post')) {
            $form->bind($request);
            if ($form->isValid()) {
                $message = $form->getData();
                $em = $this->getDoctrine()->getEntityManager();
                $repository = $em->getRepository('RSPReseauSocialBundle:message');
                $em->persist($message);
                $em->flush();
                $messages = $repository->findAll();
                return $this->render('RSPReseauSocialBundle:Message:liste.html.twig', array('Messages' => $messages));
            }
        }
        return $this->render('RSPReseauSocialBundle:Message:new.html.twig', array('form' => $form->createView()));
    }
     public function voirAction($id) {
        $em = $this->getDoctrine()->getEntityManager();
        $repository = $em->getRepository('RSPReseauSocialBundle:message');
        $message = $repository->find($id);
        return $this->render('RSPReseauSocialBundle:message:Message:voir.html.twig', array('Message' => $message));
    }

    public function deleteAction($id) {
        $em = $this->getDoctrine()->getEntityManager();
        $repository = $em->getRepository('RSPReseauSocialBundle:message');
        $message = $repository->find($id);
        $em->remove($message);
        $em->flush();

        $messages = $repository->findAll();
        return $this->render('RSPReseauSocialBundle:Message:liste.html.twig', array('Messages' => $messages));
    }

}